from _pydecimal import Decimal
import pytest
from common.setting import ensure_path_sep
from util.mysqlUtils.db_pool import Database
from util.noticeUtils.wecgat_send import WeChatSend
from util.sqlalchemy.medel import FlowReport, BundleStatistics
from util.sqlalchemy.sqlalchemy_manager import DatabaseManager
from util.times_tool.time_control import now_time

@pytest.fixture(scope="class")
def setup_fix():
    flow_report_db = Database(ensure_path_sep("\\config\\mysql_config.yaml"), "sit_union", "test")

    db_manager = DatabaseManager('mysql+pymysql://viu_root:myviu_4359@10.141.194.75:23306/test')
    db_list = [db_manager, flow_report_db]
    # 删除 flow_report / bundle_statistics 2张表的数据
    db_manager.delete_data(model=FlowReport)
    db_manager.delete_data(model=BundleStatistics)
    yield db_list
    datas = db_manager.query_data(BundleStatistics)
    text = f"""<font color='red' size='20'>**Bundle自更新agnet纳管实例可用数实时监控服务**</font>\n监控执行时间<font color='red' size='20'>{now_time()}</font>\n"""
    for data in datas:
        if data.total_num == 0:
            continue
        percent_num = Decimal(data.use_num / data.total_num * 100).quantize(Decimal('0.00'))
        text += f"{data.biz_name}——>纳管了vm{data.total_num}个 可用的vm{data.use_num}个 可用数占比<font color='red' size='20'>{percent_num} </font>%\n"
    WeChatSend("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=52ee53b6-0f41-4a76-99f8-1297de37ffc7").send_markdown(text)


@pytest.fixture(scope="class")
def init_db():
    flow_report_db = Database(ensure_path_sep("\\config\\mysql_config.yaml"), "sit_union", "test")

    db_manager = DatabaseManager('mysql+pymysql://viu_root:myviu_4359@10.141.194.75:23306/test')
    db_list = [db_manager, flow_report_db]
    # 删除 flow_report / bundle_statistics 2张表的数据
    # db_manager.delete_data(model=FlowReport)
    # db_manager.delete_data(model=BundleStatistics)
    yield db_list
    # datas = db_manager.query_data(BundleStatistics)
    # text = f"""
    # 监控执行时间<font color='red' size='20'>{now_time()}  </font> \n
    # """
    # for data in datas:
    #     percent_num = Decimal(data.use_num / data.total_num * 100).quantize(Decimal('0.00'))
    #     text += f"{data.biz_name}——>纳管了vm{data.total_num}个 可用的vm{data.use_num}个 可用数占比<font color='red' size='20'>{percent_num}  </font>% \n"
    # WeChatSend("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=52ee53b6-0f41-4a76-99f8-1297de37ffc7").send_markdown(text)